//#define OleDb

using System;
using System.Collections;
using System.Reflection;
using Eldorado.Relational;
using Eldorado.Relational.RelationalSource;
using Eldorado.Relational.SqlDriver;
using Eldorado.Data;
#if OleDb
using Eldorado.Data.OleDb;
#else
using Eldorado.Data.SqlClient;
#endif
using Eldorado.Object;
using Eldorado.Object.Relational;
using MSPetShop.DataAccess;
using NUnit.Framework;

namespace MSPetShop.NUnit
{
	public class BaseTest : Assertion
	{
		public static string ConnectionString;
		public static IObjectDomain OuvragesDomain;
		public static IObjectDomain MSPetShopDomain;

		static BaseTest()
		{
			IDataSource datasource;
			Eldorado.Data.SqlClient.FrmSqlConnection f = new Eldorado.Data.SqlClient.FrmSqlConnection();
			f.Catalog = "MSPetShop";
			if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)  
			{
#if OleDb
				ConnectionString = "Provider=SQLOLEDB;Initial Catalog=" + f.Catalog + ";" + f.ConnectionString;
				datasource = new OleDbDataSource(ConnectionString);
#else
				ConnectionString = "Initial Catalog=" + f.Catalog + ";" + f.ConnectionString;
				datasource = new SqlDataSource(ConnectionString);
#endif
			}
			else
				throw new Exception("cannot connect to database");

			IDictionary extendedProperties = new Hashtable();
			extendedProperties["DataSource"] = datasource;
			MSPetShopDomain = new ObjectDomain(Assembly.Load("MSPetShop.DataAccess"), "MSPetShop.DataAccess.Map.SqlServer.xml", extendedProperties);
		}

		public IObjectContext CreateContext()
		{
#if OleDb
			IDbRelationalSource source = new OleDbRelationalSource(ConnectionString, new TransactSqlDriver());
#else
			IDbRelationalSource source = new SqlRelationalSource(ConnectionString, new TransactSqlDriver());
#endif
			return new RelationalObjectContext(MSPetShopDomain, source);
		}

		public Account CreateAccount(IObjectContext context)
		{
			Account result = (Account)context.LocalStorage.Create(typeof(Account));
			return result;
		}

		public BannerData CreateBannerData(IObjectContext context)
		{
			BannerData result = (BannerData)context.LocalStorage.Create(typeof(BannerData));
			return result;
		}

		public Category CreateCategory(IObjectContext context)
		{
			Category result = (Category)context.LocalStorage.Create(typeof(Category));
			return result;
		}

		public Inventory CreateInventory(IObjectContext context)
		{
			Inventory result = (Inventory)context.LocalStorage.Create(typeof(Inventory));
			return result;
		}

		public Item CreateItem(IObjectContext context)
		{
			Item result = (Item)context.LocalStorage.Create(typeof(Item));
			return result;
		}

		public Product CreateProduct(IObjectContext context)
		{
			Product result = (Product)context.LocalStorage.Create(typeof(Product));
			return result;
		}

		public Profile CreateProfile(IObjectContext context)
		{
			Profile result = (Profile)context.LocalStorage.Create(typeof(Profile));
			return result;
		}

		public SignOn CreateSignOn(IObjectContext context)
		{
			SignOn result = (SignOn)context.LocalStorage.Create(typeof(SignOn));
			return result;
		}

		public Supplier CreateSupplier(IObjectContext context)
		{
			Supplier result = (Supplier)context.LocalStorage.Create(typeof(Supplier));
			return result;
		}
	}
}
